package com.ruoyi.slow.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.sale.domain.SaleorderModel;
import com.ruoyi.slow.domain.EChartsClass;
import com.ruoyi.slow.domain.Slow;
import com.ruoyi.slow.mapper.SlowMapper;
import com.ruoyi.slow.service.SlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SlowServiceImpl implements SlowService {

    @Autowired
    private SlowMapper slowMapper;

    @Override
    public List<SaleorderModel> selectSlowListOne(SaleorderModel saleorderModel)
    {
        return slowMapper.selectSlowListOne(saleorderModel);
    }

    @Override
    public List<ProductModel> selectSlowListTwo(ProductModel productModel)
    {
        return slowMapper.selectSlowListTwo(productModel);
    }

    @Override
    public List<Slow> selectSlowListWhichSpill(Slow slow)
    {
        slow.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        return slowMapper.selectSlowListWhichSpill(slow);
    }

    @Override
    public List<EChartsClass> selectEChartsList(EChartsClass eChartsClass)
    {
        return slowMapper.selectEChartsList(eChartsClass);
    }

    @Override
    public List<EChartsClass> selectEChartsSevenList(int key,String startTime,String endTime)
    {
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        EChartsClass eChartsClass=new EChartsClass();
        Long companyId = sysUser.getCompanyId();
        eChartsClass.setCompanyId(companyId);
        if (!sysUser.getDeptId().equals(companyId)){
            eChartsClass.setUserId(sysUser.getUserId());
        }
        if(key==3){
            return slowMapper.selectEChartsYearList(eChartsClass);
        }else if(key==2){
            return slowMapper.selectEChartsMonthList(eChartsClass);
        }else if(startTime!=null&&endTime!=null&& !startTime.equals("") && !endTime.equals("")){
            eChartsClass.setBeginDate(startTime);
            eChartsClass.setEndDate(endTime);
            return slowMapper.selectEChartsBetweenList(eChartsClass);
        }else{
            return slowMapper.selectEChartsSevenList(eChartsClass);
        }
}

    @Override
    public int insertSlows() {
        List<SaleorderModel> saleorderModelList=slowMapper.selectSlowListOne(new SaleorderModel());
        List<ProductModel> productModelList=slowMapper.selectSlowListTwo(new ProductModel());
        List<Slow> slowList=new ArrayList<Slow>();
        Date nowDate= DateUtils.getNowDate();
        for(SaleorderModel saleorderModel:saleorderModelList){
            Slow slow=new Slow();
            for(ProductModel productModel:productModelList){
                if(saleorderModel.getModelCode().equals(productModel.getModelCode())&& saleorderModel.getDeptId().equals(productModel.getDeptId())){
                    slow.setCompanyId(productModel.getDeptId());
                    slow.setProductId(productModel.getProductId());
                    slow.setProductType(productModel.getProductType());
                    int ratio=getMonth(productModel.getCreateTime(),nowDate);
                    if(ratio>12){
                        ratio=12;
                    }
                    int num = saleorderModel.getNum().intValue();
                    slow.setSlowNum(num*productModel.getSlowRatio()/100/ratio);
                    slowList.add(slow);
                }
            }
        }
        return slowMapper.insertSlows(slowList);
    }

    public  int getMonth(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);
        int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        if ((startCalendar.get(Calendar.DATE) == 1)&& (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }


}
